package com.eastcom.groovy.task.taskdirector.file.excel
import com.eastcom.groovy.config.TaskConfig
import com.eastcom.groovy.context.ServerContext
import com.eastcom.groovy.context.TaskContext
import com.eastcom.groovy.provider.DbContextProvider
import com.eastcom.groovy.provider.DynamicBeanDomianProvider
import com.eastcom.groovy.provider.SpringContextProvider
import com.eastcom.groovy.task.Task
import com.eastcom.groovy.tools.db.DbEngine
import com.eastcom.groovy.tools.db.DbInfo
import com.eastcom.groovy.tools.db.Table
import com.eastcom.groovy.tools.file.ExcelReader
import com.eastcom.groovy.tools.file.GroovyUtil
import com.eastcom.groovy.tools.file.JaxbJavaUtilDateSerializer;
import com.eastcom.groovy.tools.file.JaxbUtil
import com.eastcom.groovy.tools.TaskUtil
import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;

import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.beans.factory.BeanFactory
import org.springframework.beans.factory.support.BeanDefinitionRegistry
import org.springframework.beans.factory.support.DefaultListableBeanFactory
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader
import org.springframework.core.io.FileSystemResource
import org.springframework.dao.DataAccessException
import org.springframework.jdbc.core.ResultSetExtractor
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor

import java.awt.TexturePaintContext.Int;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.sql.CallableStatement
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet
import java.sql.SQLException
import java.sql.Statement;
import java.sql.Timestamp
import java.sql.Types
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ThreadPoolExecutor
import java.util.regex.Matcher
import java.util.regex.Pattern

import javax.xml.bind.annotation.XmlAccessorType
import javax.xml.bind.annotation.XmlElement
import javax.xml.bind.annotation.XmlRootElement
import javax.xml.ws.wsaddressing.W3CEndpointReference.Address;
public boolean initJob(String jobId){
}

/**
 * 鎵ц鏂规硶
 * @param jobId
 */
public boolean doJob(String jobId) {
	Log logger = LogFactory.getLog(this.class)
	DbEngine dbEngine = SpringContextProvider.getBean("dbEngine")
	dbEngine.getJdbcUtil().setDataSource(DynamicBeanDomianProvider.getBean("oracle_39_ipnet"))
	File dirs = new File("E:\\ftpfile");
	if(! dirs.isDirectory()){
		logger.debug("inviland dir!");
		return;
	}
	File[] f2 = dirs.listFiles(new FilenameFilter() {

				@Override
				public boolean accept(File dir, String name) {
					// TODO 自动生成的方法存根
					if(name.matches("mmse.xls")){
						logger.debug("recieve file:"+name);
						return true;
					}
					return false;
				}
			});
	if(f2 == null){
		logger.debug("not any file of regex name found!");
		return;
	}
	Calendar fileTime = Calendar.getInstance();
	Calendar fileLimitTime = Calendar.getInstance();
	long ft = 150000000;
	fileLimitTime.setTimeInMillis(new Date().getTime() - ft);
	
	List<List<String>> result
	for(File file2: f2){
		try {
			if(file2.lastModified() < fileLimitTime.getTimeInMillis()){
				continue
			}
			result = doQueryExcel(file2)
			if(!result.isEmpty()){
				String sql = "insert into MMS_DINGZHI_IFMERRCODE_HOUR(TIMESTAMP,ERRORCODE,ERRORCOUNT) values(?,?,?)"
				for(List<String> raw: result){
					Object[] param = new Object[3]
					SimpleDateFormat sim = new SimpleDateFormat("yyyyMMddHH")
					Timestamp ti = new Timestamp(sim.parse(raw.get(0)).getTime())
					Integer errorCode = Integer.valueOf(raw.get(1))
					Integer errorNum = Integer.valueOf(raw.get(2))
					param[0] = ti
					param[1] = errorCode
					param[2] = errorNum
					int[] type = new int[3]
					type[0] = Types.TIMESTAMP
					type[1] = Types.NUMERIC
					type[2] = Types.NUMERIC
					dbEngine.getJdbcUtil().update(sql, param,type)
				}
			}
		} catch (Exception e) {
			e.printStackTrace()
		}
	}
}


public List<List<String>> doQueryExcel(File fl){
	StringBuffer sqlBuffer = new StringBuffer();
	String fileName = fl.getName();
	List<List<String>> tempResult = new ArrayList()
	List<List<String>> resultList = new ArrayList()
	ExcelReader excelReader = new ExcelReader();
	List<String[]> fileContext = excelReader.readExcelContent(new FileInputStream(fl));

	fileContext.each {
		List<String> raw = new ArrayList()
		it.each {
			raw.add(it)
		}
		tempResult.add(raw)
	}
	List<String> time = tempResult.get(1).subList(1, tempResult.get(2).size())
	for(int i = 0; i < time.size(); i++){
		String t = time.get(i)
		for(int n = 2; n < tempResult.size(); n ++){
			List<String> raw = tempResult.get(n)
			List<String> rawA = new ArrayList()
			resultList.add(rawA)
			rawA.add(t)
			for(int j = 0; j < i+2; j++){
				rawA.add(raw.get(j))
			}
		}

	}
	resultList.each {
		it.each {
			print it+","
		}
		println " "
	}
	return resultList
}

